package com.yunkaweilai.android.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.yunkaweilai.android.R;
import com.yunkaweilai.android.interfaces.GoodsCaozuoInterfaces;
import com.yunkaweilai.android.model.operation.CreateSelectedGoodsModel;
import com.yunkaweilai.android.model.shop.ShopSkuInfoModel;
import com.yunkaweilai.android.utils.MoneyTextWatcher;
import com.yunkaweilai.android.utils.Utils;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by Administrator on 2018/7/4.
 */

public class GoodsCaoZuoDialog extends Dialog {

    private EditText idEdtPrice;
    private TextView idTvZs,idTvGj,idTvHy;
    private ShopSkuInfoModel shopSkuInfoModel;
    private CreateSelectedGoodsModel.DataBean.GoodsListBean item;
    private GoodsCaozuoInterfaces goodsCaozuoInterfaces;
    public GoodsCaoZuoDialog(@NonNull Context context, ShopSkuInfoModel shopSkuInfoModel, CreateSelectedGoodsModel.DataBean.GoodsListBean item, GoodsCaozuoInterfaces goodsCaozuoInterfaces) {
        super(context, R.style.dialog);
        this.item = item;
        this.goodsCaozuoInterfaces = goodsCaozuoInterfaces;
        this.shopSkuInfoModel = shopSkuInfoModel;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_goods_caozuo);
        idEdtPrice = ((EditText) findViewById(R.id.id_edt_price));
        idTvZs = ((TextView) findViewById(R.id.id_tv_zs));
        idTvGj = ((TextView) findViewById(R.id.id_tv_gj));
        idTvHy = ((TextView) findViewById(R.id.id_tv_hy));
        idEdtPrice.setText(item.getShop_price()+"");
//        SpannableString ss = new SpannableString("输入改价金额");//定义hint的值
//        AbsoluteSizeSpan ass = new AbsoluteSizeSpan(20,true);//设置字体大小 true表示单位是sp
//        ss.setSpan(ass, 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//        idEdtPrice.setHint(new SpannedString(ss));
        idEdtPrice.addTextChangedListener(new MoneyTextWatcher(idEdtPrice));
//        idEdtPrice.setSelection(idEdtPrice.length());
        findViewById(R.id.id_img_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        initClick();
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            public void run(){
                InputMethodManager inputManager = (InputMethodManager) idEdtPrice.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.showSoftInput(idEdtPrice, 0);
            }
        },800);
    }

    private void initClick() {
        idTvZs.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                shopSkuInfoModel.setChange_price("-1");
                shopSkuInfoModel.setIs_give("1");
                item.setChange_price("-1");
                item.setIs_give("1");
                goodsCaozuoInterfaces.caozuo();
                dismiss();
            }
        });
        idTvGj.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Utils.isEmpty(idEdtPrice.getText().toString())){
                    shopSkuInfoModel.setChange_price("0");
                    shopSkuInfoModel.setIs_give("0");
                    item.setChange_price("0");
                    item.setIs_give("0");
                }else{
                    shopSkuInfoModel.setChange_price(idEdtPrice.getText().toString());
                    shopSkuInfoModel.setIs_give("0");
                    item.setChange_price(idEdtPrice.getText().toString());
                    item.setIs_give("0");
                }
                goodsCaozuoInterfaces.caozuo();
                dismiss();
            }
        });
        idTvHy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                shopSkuInfoModel.setChange_price("-1");
                shopSkuInfoModel.setIs_give("0");
                item.setChange_price("-1");
                item.setIs_give("0");
                goodsCaozuoInterfaces.caozuo();
                dismiss();
            }
        });

    }
}
